const {Controller} = require('egg');
const jwt = require('jsonwebtoken');

class UserController extends Controller{
    //登录
    async login(){
        let {ctx,service} = this;
        //1.接收参数
        let {username,password} = ctx.request.body;
        //2.容错
        if(username && password){
            let target = await service.user.login(username,password);  //[{}]

            if(target.length){
                let token = jwt.sign({username,password},'lixd',{expiresIn:60});
                ctx.body = {
                    code:1,
                    msg:'登录成功',
                    token
                }
            }else{
                ctx.body = {
                    code:0,
                    msg:'登录失败'
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }

    }
}

module.exports = UserController